package com.jianghang.class_three.utils;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class StringUtil {

    /**
     * 获取字符串第n个分隔符所在的位置
     * 例如 获取"sffb"fdfdg"vdfd"fdfd" 第2个"的位置为4
     *
     * @param str      输入的字符串
     * @param operator 分隔符号
     * @param num      第几个分隔符
     * @return
     */
     static int getCharPosition(String str, String operator, int num) {
        Matcher matcher = Pattern.compile(operator).matcher(str);
        int count = 0;
        while (matcher.find()) {
            count++;
            if (count == num) {
                break;
            }
        }
        return matcher.start();
    }

    public static void main(String[] args) {
        String str = "10.100.0.1 - - [10/Nov/2016:00:01:02 +0800] \"HEAD / HTTP/1.1\" 301 0 \"117.121.101.40\" \"-\" - \"curl/7.19.7 (x86_64-redhat-linux-gnu) libcurl/7.19.7 NSS/3.16.2.3 Basic ECC zlib/1.2.3 libidn/1.18 libssh2/1.4.2\" \"-\" - - - 0.000";

        System.out.println(getCharPosition(str, "\"", 7));
    }
}
